// 模拟双端队列  double-end-queue
class deQueue {
  items = {};
  lowCount = 0;
  count = 0;
  addEnd(item) {
    this.items[this.count] = item;
    this.count++;
  }
  removeEnd() {
    if (this.isEmpty()) {
      return;
    }
    const removeEndItem = this.items[this.count - 1];
    delete this.items[this.count - 1];
    this.count--;
    return removeEndItem;
  }
  addFront(item) {
    if (this.isEmpty()) {
      this.addEnd(item);
    } else {
      if (this.lowCount > 0) {
        this.lowCount--;
        this.items[this.lowCount] = item;
      } else {
        for (let i = this.count; i > 0; i--) {
          this.items[i] = this.items[i - 1];
        }
        this.items[0] = item;
        this.count++;
      }
    }
  }
  removeFront() {
    if (this.isEmpty()) {
      return;
    }
    const removeFrontItem = this.items[this.lowCount];
    delete this.items[this.lowCount];
    this.lowCount++;
    return removeFrontItem;
  }
  peekFront() {
    return this.items[this.lowCount];
  }
  peekEnd() {
    return this.items[this.count - 1];
  }
  isEmpty() {
    return this.size() === 0;
  }
  size() {
    return this.count - this.lowCount;
  }
  clear() {
    this.items = {};
    this.lowCount = 0;
    this.count = 0;
  }
}

const dequeue = new deQueue()

dequeue.addFront(100)
dequeue.addFront(200)
dequeue.addEnd(300)
dequeue.addFront(400)
dequeue.addFront(500)
console.log(dequeue);
console.log(dequeue.removeFront());
console.log(dequeue.removeEnd());
console.log(dequeue.peekFront());
console.log(dequeue.peekEnd());
console.log(dequeue.removeFront());
console.log(dequeue.removeFront());
console.log(dequeue.removeFront());
console.log(dequeue.removeFront());
console.log(dequeue.removeFront());